//package com.pengzy.core.gateway_security;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.pengzy.comment.core.jwt.JwtTokenUtils;
//import com.pengzy.comment.mybatis.entity.SysUser;
//import com.pengzy.comment.mybatis.mapper.SysMenuMapper;
//import com.pengzy.comment.mybatis.mapper.SysUserMapper;
//import com.pengzy.supports.dto.LoginUserDetails;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.crypto.factory.PasswordEncoderFactories;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.stereotype.Service;
//import reactor.core.publisher.Mono;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.Map;
//import java.util.Objects;
//
///**
// * @author xiaozhi 2023/7/17
// */
//@Slf4j
//@Service
//public class UserService implements ReactiveUserDetailsService {
//
//    @Resource
//    private SysUserMapper sysUserMapper;
//    @Resource
//    private SysMenuMapper sysMenuMapper;
//
//    /**
//     * 获取数据库的用户信息与登录表单信息进行验证
//     * 进入此方法只能是调用登录接口
//     * @param username 登录页面输入的账号
//     * @return 数据库用户信息
//     */
//    @Override
//    public Mono<UserDetails> findByUsername(String username) {
//        return Mono.just(queryDbUser(username));
//    }
//
//
//    /**
//     * 获取redis看下有没有此jwt，如果没有则抛异常 用户重新登录
//     * @param jwt
//     * @return
//     */
//    public Mono<UserDetails> findByUsernameWithCache(String jwt) throws Exception {
//        Map<String, Object> map = JwtTokenUtils.parseTokenToMap(jwt);
//        UserDetails build = User.withUsername((String) map.get("user"))
//                .password("非登录校验，设置任意值，因为此值后续用不到")
//                .roles((String) map.get("roles"))
//                .build();
//        return Mono.just(build);
//    }
//
//
//    public Mono<UserDetails> findByUsernameWithSom(LoginUserDetails loginUserDetails) throws Exception {
//        UserDetails build = User.withUsername(loginUserDetails.getUsername())
//                .password("非登录校验，设置任意值，因为此值后续用不到")
//                .authorities(loginUserDetails.getAuthorities())
//                .build();
//        return Mono.just(build);
//    }
//
//
//
//    /**
//     * 查询数据库
//     * @param username
//     * @return
//     */
//    private LoginUserDetails queryDbUser(String username){
//        //PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
//        //根据用户名查询用户信息
//        SysUser user=getUser(username);
//        //UserDetail detail= BeanUtil.toBean(user, UserDetail.class);
//        //TODO 根据用户查询权限信息 添加到LoginUser中
//        List<String> list=sysMenuMapper.menuList(user.getUserId());
//        LoginUserDetails loginUserDetails=new LoginUserDetails();
//        loginUserDetails.setUsername(user.getUserName());
//        loginUserDetails.setPermissions(list);
//        loginUserDetails.setPassword(user.getPassword());
//        loginUserDetails.setUser(user);
//
////        userDetails = User.withUsername(username)
////                .password(detail.getPassword())
////                .authorities(list.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()))
////                .build();
//        return loginUserDetails;
//    }
//
//    public static void main(String[] args) {
//        String pass = "admin123";
//        PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
//        //BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        final String passHash = encoder.encode(pass);
//        System.out.println(passHash);
//
//        final boolean matches = encoder.matches(pass, passHash);
//        System.out.println(matches);
//    }
//
//
//
//    public SysUser getUser(String username){
//        LambdaQueryWrapper<SysUser> userLambdaQueryWrapper= Wrappers.lambdaQuery();
//        userLambdaQueryWrapper.eq(SysUser::getUserName,username)
//                .last("limit 1");
//        SysUser sysUser = sysUserMapper.selectOne(userLambdaQueryWrapper);
//        if(Objects.isNull(sysUser)){
//            throw new RuntimeException("用户或者密码为错误！");
//        }
//        return sysUser;
//    }
//
//}
